package com.example.demo.controller;

import com.example.demo.dto.TaskApprovalStatusDTO;
import com.example.demo.dto.MultipleApprovalResultDTO;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.TaskRoutingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/task-routing")

public class TaskRoutingController {

    @Autowired
    private TaskRoutingService taskRoutingService;

//    @Operation(summary = "为单个任务分配审批人", description = "根据任务数据和流程节点信息进行路由，分配审批人")
    @PostMapping("/assign/{taskApprovalStatusId}")
    public ResponseEntity<ApiResponse<TaskApprovalStatusDTO>> assignApprover(
             @PathVariable Long taskApprovalStatusId,
            HttpServletRequest request) {

        //log.info("开始为任务审批状态ID: {} 分配审批人", taskApprovalStatusId);

        try {
            TaskApprovalStatusDTO result = taskRoutingService.assignApprover(taskApprovalStatusId);

            return ResponseEntity.ok(
                    ApiResponse.success(result, "审批人分配成功", request.getRequestURI(), null));

        } catch (Exception e) {
            //log.error("为任务审批状态ID: {} 分配审批人失败: {}", taskApprovalStatusId, e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("审批人分配失败: " + e.getMessage(), request.getRequestURI()));
        }
    }

//    @Operation(summary = "批量分配审批人（按流程）", description = "为指定流程的所有任务批量分配审批人")
    @PostMapping("/batch-assign/flow/{taskFlowId}")
    public ResponseEntity<ApiResponse<Integer>> batchAssignApproversByFlow(
             @PathVariable Long taskFlowId,
            HttpServletRequest request) {

        //log.info("开始为流程ID: {} 批量分配审批人", taskFlowId);

        try {
            int assignedCount = taskRoutingService.batchAssignApprovers(taskFlowId);

            return ResponseEntity.ok(
                    ApiResponse.success(assignedCount,
                            String.format("成功为 %d 个任务分配审批人", assignedCount),
                            request.getRequestURI(), null));

        } catch (Exception e) {
            //log.error("为流程ID: {} 批量分配审批人失败: {}", taskFlowId, e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("批量分配审批人失败: " + e.getMessage(), request.getRequestURI()));
        }
    }

//    @Operation(summary = "批量分配审批人（按数据源）", description = "为指定数据源的所有任务批量分配审批人")
    @PostMapping("/batch-assign/datasource/{dataSourceId}")
    public ResponseEntity<ApiResponse<Integer>> batchAssignApproversByDataSource(
             @PathVariable Long dataSourceId,
            HttpServletRequest request) {

        //log.info("开始为数据源ID: {} 批量分配审批人", dataSourceId);

        try {
            int assignedCount = taskRoutingService.batchAssignApproversByDataSource(dataSourceId);

            return ResponseEntity.ok(
                    ApiResponse.success(assignedCount,
                            String.format("成功为 %d 个任务分配审批人", assignedCount),
                            request.getRequestURI(), null));

        } catch (Exception e) {
            //log.error("为数据源ID: {} 批量分配审批人失败: {}", dataSourceId, e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("批量分配审批人失败: " + e.getMessage(), request.getRequestURI()));
        }
    }
    
//    @Operation(summary = "为任务分配多个审批人并根据负载均衡选择", description = "根据任务数据和流程节点信息进行路由，获取所有匹配的审批人并根据负载均衡选择最合适的审批人")
    @PostMapping("/assign-multiple/{taskApprovalStatusId}")
    public ResponseEntity<ApiResponse<MultipleApprovalResultDTO>> assignMultipleApprovers(
             @PathVariable Long taskApprovalStatusId,
            HttpServletRequest request) {

        //log.info("开始为任务审批状态ID: {} 分配多个审批人并根据负载均衡选择", taskApprovalStatusId);

        try {
            MultipleApprovalResultDTO result = taskRoutingService.assignMultipleApprovers(taskApprovalStatusId);

            return ResponseEntity.ok(
                    ApiResponse.success(result, "多审批人分配成功", request.getRequestURI(), null));

        } catch (Exception e) {
            //log.error("为任务审批状态ID: {} 分配多个审批人失败: {}", taskApprovalStatusId, e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("多审批人分配失败: " + e.getMessage(), request.getRequestURI()));
        }
    }
}